<template>
  <j-modal
    :confirmLoading="confirmLoading"
    :dialogStyle="dialogStyle"
    :title="title"
    :visible="visible"
    :width="700"
    cancelText="关闭"
    switchFullscreen
    @cancel="handleCancel"
    @ok="handleOk">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
<!--        创建用户-->
<!--        {-->
<!--        "name": "姓名",-->
<!--        "password": "密码",-->
<!--        "email": "邮箱",-->
<!--        "type": 用户类型：1=本地用户 2=AD用户,-->
<!--        "status": 用户状态：1=启用 2=禁用,-->
<!--        }-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="姓名">
          <a-input v-decorator="['name', {
           rules: [
              {
                required: true,
               message: '姓名不能为空！',
              },
            ],
            }]" placeholder="请输入姓名" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="密码">
          <a-input v-decorator="['password', {
           rules: [
              {
                required: true,
                message: '密码不能为空！',
              },
            ],
            }]" placeholder="请输入密码" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="邮箱">
          <a-input v-decorator="['email', {
           rules: [
              {
                required: true,
                message: '邮箱不能为空！',
              },
            ],
            }]" placeholder="请输入邮箱" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="用户类型">
          <a-select v-decorator="['type', {
            rules: [
              {
                required: true,
                message: '用户类型不能为空!',
              },
            ],
            }]" placeholder="请选择用户类型" @change="handleChange">
            <a-select-option v-for="(i,index) in typeList" :key="index" :value="i.key">{{ i.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="用户状态">
          <a-select v-decorator="['status', {
            rules: [
              {
                required: true,
                message: '用户状态不能为空!',
              },
            ],
            }]" placeholder="请选择用户状态" @change="handleChange">
            <a-select-option v-for="(i,index) in statusList" :key="index" :value="i.key">{{ i.name }}
            </a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import pick from 'lodash.pick'
import { httpAction, postAction } from '@api/manage'
import moment from 'moment'
export default {
  name: 'userModal',
  data() {
    return {
      typeList: [
        { name: '本地用户 ', key: '1' },
        { name: '外地', key: '2' },
      ],
      confirmLoading: false,
      title: '',
      visible: false,
      form: this.$form.createForm(this),
      currentTime: '',
      dialogStyle: {
        top: '300px'
        // right:"20px",
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
     // "statusList": 用户状态：1=启用 2=禁用,
      statusList: [
        { name: '启用', key: '1' },
        { name: '禁用', key: '2' },
      ],
      url: {
        // add: '/cxm/cmsArticle/add',
        // edit: '/cxm/cmsArticle/edit'
      }
    }
  },
  methods: {
    getCurrentData() {
      if (this.currentTime === '') {
        this.currentTime = moment().format('YYYY-MM-DD')
      }
    },
    add() {
      this.edit({})
    },
    edit(record) {
      console.log(record)
      this.visible = true
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(
          this.model,
          'name',
          'password',
          'email',
          'type',
          'status'
        ))
      })
    },
    handleChange(value) {
      console.log(value)
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    handleOk() {
      let that = this
      this.form.validateFields((err, values) => {
        if (!err) {
          let httpurl = ''
          let method = ''
          this.confirmLoading = true
          console.log(this.model)
          if (!this.model.id) {
            httpurl += this.url.add
          } else {
            httpurl += this.url.edit
          }
          method = 'post'
          console.log(httpurl)
          let formData = Object.assign(this.model, values)
          // httpAction(httpurl, formData, method).then(res => {
          //   if (res.success) {
          //     that.$message.success(res.message)
          //     that.$emit('refresh')
          //   } else {
          //     that.$message.error(res.message)
          //   }
          // }).finally(() => {
          //   that.confirmLoading = false
          //   that.close()
          //   that.$parent.$parent.loadData()
          // })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>